package cn.jbit.smbms.controller;

import cn.jbit.smbms.pojo.User;
import cn.jbit.smbms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.lang.invoke.MethodType;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    //访问欢迎页面
    @RequestMapping("/welcome")
    public String welcome(){
        return "welcome";
    }
    //访问登录页面
//    @GetMapping({"/","login"})
//    public String login(){
//        return "/login";
//    }
    @RequestMapping( "/login")
    public String login(){
        return "login";
    }

    //登录
    @PostMapping("/dologin")
    public String login(String userCode, String userPassword, HttpSession session){
        User user=userService.login(userCode,userPassword);
        if(null!=user){
            session.setAttribute("loginUser",user);
            return "redirect:/welcome";
        }else{
            //bug
            session.setAttribute("message","用户名或密码错误！");
            return "redirect:/login";
        }
    }
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login";
    }

}
